package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.pojo.Appointment;
import com.zhentao.service.AppointmentService;
import com.zhentao.mapper.AppointmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.regex.Pattern;

/**
 * @author HP
 * @description 针对表【appointment】的数据库操作Service实现
 * @createDate 2025-03-22 09:10:45
 */
@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements AppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public String add(Appointment appointment) {
        boolean validIdCard = isValidIdCard(appointment.getSfz());
        if(!validIdCard){
            return "请注意身份证号格式!!";
        }
        int i = appointmentMapper.insert(appointment);
        if(i>0){
            return "预约成功";
        }
        return "预约失败";
    }

    /**
     * 校验身份证号是否有效
     * @param idCard 身份证号
     * @return 如果身份证号有效返回true，否则返回false
     */
    public boolean isValidIdCard(String idCard) {
        if (idCard == null || idCard.length() != 18) {
            return false;
        }
        String regex = "^[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[0-9Xx]$";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(idCard).matches();
    }
}
